﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aribaccio;
using Aribaccio.Device;
using BDPAutodetect.DataService;
using BDPAutodetect.Device;
using BDPAutodetect.Hardware;

namespace BDPAutodetect.Mgrcore
{
    /// <summary>
    /// 检测设备管理
    /// </summary>
    public class DetectDeviceManager : BaseManager
    {
        /// <summary>
        /// 设备管理实例
        /// </summary>
        protected DeviceManager DeviceMgr { get; set; }
        /// <summary>
        /// 指标设备协议管理实例
        /// </summary>
        protected TargetSlaveManager TargetSlaveMgr { get; set; }
        /// <summary>
        /// 检测设备集合
        /// </summary>
        public List<DetectDeviceBase> DetectDevices { get; protected set; }

        /// <summary>
        /// 构造函数[注入]
        /// </summary>
        public DetectDeviceManager(DeviceManager argDeviceManager, TargetSlaveManager argTargetSlaveManager)
            : base("检测设备管理中心")
        {
            this.DeviceMgr = argDeviceManager;
            this.TargetSlaveMgr = argTargetSlaveManager;
            this.DetectDevices = new List<DetectDeviceBase>();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns></returns>
        public override async Task Initialize()
        {
            if (this.DeviceMgr.Count <= 0) return;
            var reulstTask = Task.Run(() =>
            {
                //构建检测设备
                foreach (var deviceItem in this.DeviceMgr.Devices)
                {
                    var deviceType = deviceItem.DevicePara.DeviceType.ToEnum<DeviceType>();
                    switch (deviceType)
                    {
                        case DeviceType.Customer:
                            int nStation = deviceItem.DevicePara.ModuleType.ToInt();
                            this.DetectDevices.Add(new DetectDeviceCustomer(nStation.ToEnum<DeviceStationType>(), (DeviceSerialAscii<IDevicePara>)deviceItem));
                            break;
                        case DeviceType.Electrical:
                            this.DetectDevices.Add(new DetectDeviceElectrical((DeviceSerialModbus<IDevicePara>)deviceItem, TargetSlaveMgr.GetTargetSlaves(deviceType)));
                            break;
                        case DeviceType.SensorSign:
                            this.DetectDevices.Add(new DetectDeviceSensorSign((DeviceSerialModbus<IDevicePara>)deviceItem, TargetSlaveMgr.GetTargetSlaves(deviceType)));
                            break;
                    }
                }
                //所有检测设备初始化
                foreach (var item in this.DetectDevices)
                {
                    item.Initialize();
                }
            });
            await reulstTask.ConfigureAwait(false);
        }

        /// <summary>
        /// 获取检测设备
        /// </summary>
        /// <param name="argDeviceType">设备类型</param>
        /// <returns></returns>
        public DetectDeviceBase? GetDetectDevice(DeviceType argDeviceType)
        {
            if (this.DetectDevices == null || this.DetectDevices.Count <= 0) return null;
            return this.DetectDevices.FirstOrDefault(x => x.DeviceType == argDeviceType);
        }

        /// <summary>
        /// 获取检测设备
        /// </summary>
        /// <param name="argDeviceType">设备类型</param>
        /// <param name="argStationType">设备工位类型</param>
        /// <returns></returns>
        public DetectDeviceBase? GetDetectDevice(DeviceType argDeviceType, DeviceStationType argStationType)
        {
            if (this.DetectDevices == null || this.DetectDevices.Count <= 0) return null;
            return this.DetectDevices.FirstOrDefault(x => x.DeviceType == argDeviceType && x.StationType == argStationType);
        }
    }
}
